<!-- @format -->

<template>
  <transition name="dialog-fade">
    <div v-if="visible" class="dialog-401-overlay" @click.self="handleClose">
      <div class="dialog-401-container">
        <div class="dialog-401-header">
          <h3 class="dialog-401-title">登录已过期</h3>
          <div class="dialog-401-close" @click="handleClose">
            <i class="el-icon-close">×</i>
          </div>
        </div>

        <div class="dialog-401-content">
          <div class="dialog-401-icon">
            <i class="el-icon-warning"></i>
          </div>
          <div class="dialog-401-message">
            <p>您的登录状态已过期</p>
            <p class="dialog-401-submessage">请重新登录以继续访问</p>
          </div>
        </div>

        <div class="dialog-401-footer">
          <button
            class="dialog-401-btn dialog-401-btn-cancel"
            @click="handleCancel"
          >
            取消
          </button>
          <button
            class="dialog-401-btn dialog-401-btn-confirm"
            @click="handleConfirm"
          >
            重新登录
          </button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { useUserStore } from '@/store/user';
  const { clearUserInfo } = useUserStore();

  interface DialogProps {
    visible?: boolean;
    onConfirm?: () => void;
    onCancel?: () => void;
    redirectUrl?: string;
  }

  const props = withDefaults(defineProps<DialogProps>(), {
    visible: false,
    onConfirm: () => {},
    onCancel: () => {},
    redirectUrl: '/login',
  });

  const router = useRouter();
  const visible = ref(props.visible);

  const handleConfirm = () => {
    visible.value = false;
    props.onConfirm();
    // 默认跳转到登录页
    clearUserInfo();
    router.push(props.redirectUrl);
  };

  const handleCancel = () => {
    visible.value = false;
    props.onCancel();
  };

  const handleClose = () => {
    handleCancel();
  };

  onMounted(() => {
    visible.value = true;
  });

  defineExpose({
    visible,
    handleConfirm,
    handleCancel,
    handleClose,
  });
</script>

<style lang="scss" scoped>
  .dialog-401-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
  }

  .dialog-401-container {
    width: 420px;
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    animation: dialog-zoom-in 0.3s ease-out;
  }

  .dialog-401-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid #f0f0f0;
  }

  .dialog-401-title {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #333;
  }

  .dialog-401-close {
    cursor: pointer;
    font-size: 20px;
    color: #999;
    transition: color 0.2s;

    &:hover {
      color: #666;
    }
  }

  .dialog-401-content {
    padding: 30px 24px;
    display: flex;
    align-items: center;
  }

  .dialog-401-icon {
    margin-right: 20px;

    i {
      font-size: 48px;
      color: #e6a23c;
    }
  }

  .dialog-401-message {
    flex: 1;

    p {
      margin: 0;
      font-size: 16px;
      color: #333;
      line-height: 1.5;
    }

    .dialog-401-submessage {
      margin-top: 8px;
      font-size: 14px;
      color: #666;
    }
  }

  .dialog-401-footer {
    padding: 16px 24px 24px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
  }

  .dialog-401-btn {
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
    outline: none;

    &-cancel {
      background-color: #f5f5f5;
      color: #666;

      &:hover {
        background-color: #e8e8e8;
      }
    }

    &-confirm {
      background-color: #e6a23c;
      color: #fff;

      &:hover {
        background-color: #d69730;
      }
    }
  }

  .dialog-fade-enter-active,
  .dialog-fade-leave-active {
    transition: opacity 0.3s;
  }

  .dialog-fade-enter-from,
  .dialog-fade-leave-to {
    opacity: 0;
  }

  @keyframes dialog-zoom-in {
    from {
      transform: scale(0.9);
      opacity: 0;
    }
    to {
      transform: scale(1);
      opacity: 1;
    }
  }

  @media screen and (max-width: 768px) {
    .dialog-401-container {
      width: 90%;
      max-width: 420px;
    }

    .dialog-401-content {
      padding: 20px 24px;
    }

    .dialog-401-icon i {
      font-size: 40px;
    }
  }
</style>
